import { BaseResultType } from '#/request'
import useDealResult from '@/hooks/dealResult'
import uniRequest from '@/request'
import useAccountStore from '@/store/account'
import useUserInfoStore from '@/store/userInfo'
const { dealResult, checkResult } = useDealResult()
const getParams = (e: any,loginRes: any) => ({
  code: loginRes.code,
  wxapp_id: useAccountStore().accountDetails.wxapp_id,
  encryptedData: e.detail.encryptedData,
  iv: e.detail.iv,
  userId: useUserInfoStore().getUserInfo.userId,
  encrypted_data: e.detail.encryptedData,
  deviceId: uni.getStorageSync('deviceId'),
})
const getLoginParams = (loginRes: any) => ({
  code: loginRes.code,
  wxapp_id: useAccountStore().accountDetails.wxapp_id,
  userId: useUserInfoStore().getUserInfo.userId,
  deviceId: uni.getStorageSync('deviceId'),
})
export const userLogin = async (loginRes: any): Promise<BaseResultType<any> | null> => {
  const [result, err] = await uniRequest.post('/wxapi/user/login', getLoginParams(loginRes))
  return Promise.resolve(dealResult(err, result))
}
export const getUserPhoneNumber = async (e: any,loginRes: any): Promise<BaseResultType<any> | null> => {
  const [result, err] = await uniRequest.post('/wxapi/user/getUserPhoneNumber', getParams(e,loginRes))
  return Promise.resolve(dealResult(err, result))
}
export const selectUserInfo = async (deviceId?: string): Promise<BaseResultType<any> | null> => {
  const [result, err] = await uniRequest.get('/wxapi/user/index/detail?deviceId=' + deviceId, { token: uni.getStorageSync('token') })
  return Promise.resolve(dealResult(err, result))
}
export const updateUserInfo = async (param: any): Promise<BaseResultType<any> | null> => {
  const [result, err] = await uniRequest.get('/wxapi/user/index/editDetail', param)
  return Promise.resolve(dealResult(err, result))
}

export const getLoginRes = () =>
  new Promise<any>((resolve, reject) => {
    uni.login({
      provider: 'weixin',
      success: resolve,
      fail: reject,
    })
  })
